package com.huikeportal.controller.common;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import com.huikeportal.model.Language;
import com.huikeportal.model.ReportGraphData;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.SiteIndex;
import com.huikeportal.model.common.IndexPermission;

/**
 * 工具类
 * 
 * @author XIAKUN
 * 
 */
public class Tools {

	/**
	 * 场所list拼接成ID字符串,一般查询场所sql中用find_in_set时用到
	 * 
	 * @param siteList
	 * @return
	 */
	public static String pinjieId(List<SiteAllInfo> siteList) {
		String string = "";
		StringBuffer s = new StringBuffer("");
		for (int i = 0; i < siteList.size(); i++) {
			s.append(siteList.get(i).getSiteid() + ",");
		}
		string = s.toString();
		if (!"".equals(string)) {
			string = string.substring(0, string.length() - 1);
		}
		return string;
	}

	/**
	 * 场所list拼接成名称字符串,一般页面显示多个场所时用到
	 * 
	 * @param siteList
	 * @return
	 */
	public static String pinjieName(List<SiteAllInfo> siteList) {
		String string = "";
		StringBuffer s = new StringBuffer("");
		for (int i = 0; i < siteList.size(); i++) {
			s.append(siteList.get(i).getSitename() + "、");
		}
		string = s.toString();
		if (!"".equals(string)) {
			string = string.substring(0, string.length() - 1);
		}
		return string;
	}

	/**
	 * 拼接字符串
	 * 
	 * @param stringList
	 * @return
	 */
	public static String pinjieString(List<String> stringList) {
		String string = "";
		StringBuffer s = new StringBuffer("");
		for (int i = 0; i < stringList.size(); i++) {
			s.append(stringList.get(i) + ",");
		}
		string = s.toString();
		if (!"".equals(string)) {
			string = string.substring(0, string.length() - 1);
		}
		return string;
	}

	/**
	 * 初始化用户所有指标权限
	 * 
	 * @param indexList
	 * @return
	 */
	public static IndexPermission initIndex(List<String> stringList,
			String sysLang) {
		IndexPermission index = new IndexPermission();
		Language lang = Lang.getInfo(sysLang);
		for (int i = 0; i < stringList.size(); i++) {
			// 如果有客流量权限
			if ("0".equals(stringList.get(i))) {
				index.setIndex0Name(lang.getTraffic());
				index.setIndex0Flag("Y");
			}
			// 如果有集客力权限
			if ("1".equals(stringList.get(i))) {
				index.setIndex1Name(lang.getJkl());
				index.setIndex1Flag("Y");
			}
			// 如果有人效权限
			if ("2".equals(stringList.get(i))) {
				index.setIndex2Name(lang.getRenxiao());
				index.setIndex2Flag("Y");
			}
			// 如果有销售额权限
			if ("3".equals(stringList.get(i))) {
				index.setIndex3Name(lang.getPos());
				index.setIndex3Flag("Y");
			}
			// 如果有提袋率权限
			if ("4".equals(stringList.get(i))) {
				index.setIndex4Name(lang.getTdl());
				index.setIndex4Flag("Y");
			}
			// 如果有客单价权限
			if ("5".equals(stringList.get(i))) {
				index.setIndex5Name(lang.getKdj());
				index.setIndex5Flag("Y");
			}
			// 如果有坪效权限
			if ("6".equals(stringList.get(i))) {
				index.setIndex6Name(lang.getPingxiao());
				index.setIndex6Flag("Y");
			}
			// 如果有顾客数权限
			if ("7".equals(stringList.get(i))) {
				index.setIndex7Name(lang.getNumOfCustom());
				index.setIndex7Flag("Y");
			}
			// 如果有平均游逛时间权限
			if ("8".equals(stringList.get(i))) {
				index.setIndex8Name(lang.getAveTimeWand());
				index.setIndex8Flag("Y");
			}
			// 如果有新顾客数权限
			if ("9".equals(stringList.get(i))) {
				index.setIndex9Name(lang.getNewCusNum());
				index.setIndex9Flag("Y");
			}
			// 如果有老顾客数权限
			if ("10".equals(stringList.get(i))) {
				index.setIndex10Name(lang.getOldCusNu());
				index.setIndex10Flag("Y");
			}
			// 如果有新顾客占比权限
			if ("11".equals(stringList.get(i))) {
				index.setIndex11Name(lang.getNewCusPer());
				index.setIndex11Flag("Y");
			}
			// 如果有老顾客占比权限
			if ("12".equals(stringList.get(i))) {
				index.setIndex12Name(lang.getOldCusPer());
				index.setIndex12Flag("Y");
			}
			// 如果有进店率权限
			if ("13".equals(stringList.get(i))) {
				index.setIndex13Name(lang.getJindianlv());
				index.setIndex13Flag("Y");
			}
			// 如果有返店率权限
			if ("14".equals(stringList.get(i))) {
				index.setIndex14Name(lang.getFandianlv());
				index.setIndex14Flag("Y");
			}
			// 如果有流失量权限
			if ("15".equals(stringList.get(i))) {
				index.setIndex15Name("流失量");
				index.setIndex15Flag("Y");
			}
			// 如果有流失率权限
			if ("16".equals(stringList.get(i))) {
				index.setIndex16Name(lang.getLossAvg());
				index.setIndex16Flag("Y");
			}
			// 如果有回流量权限
			if ("17".equals(stringList.get(i))) {
				index.setIndex17Name(lang.getBackAmt());
				index.setIndex17Flag("Y");
			}
			// 如果有回流率权限
			if ("18".equals(stringList.get(i))) {
				index.setIndex18Name(lang.getBackAvg());
				index.setIndex18Flag("Y");
			}
			// 如果有会员人数权限
			if ("19".equals(stringList.get(i))) {
				index.setIndex19Name(lang.getMembership());
				index.setIndex19Flag("Y");
			}
			// 如果有会员人数占比权限
			if ("20".equals(stringList.get(i))) {
				index.setIndex20Name(lang.getMembershipPer());
				index.setIndex20Flag("Y");
			}
			// 如果有平均滞留时间权限
			if ("21".equals(stringList.get(i))) {
				index.setIndex21Name(lang.getPjzlsj());
				index.setIndex21Flag("Y");
			}
			// 如果有滞留量权限
			if ("22".equals(stringList.get(i))) {
				index.setIndex22Name("滞留量");
				index.setIndex22Flag("Y");
			}
			// 如果有出客流量权限
			if ("23".equals(stringList.get(i))) {
				index.setIndex23Name("出客流量");
				index.setIndex23Flag("Y");
			}
			// 新增于2015年12月28日09:44:56
			// 如果有活跃用户数权限
			if ("24".equals(stringList.get(i))) {
				index.setIndex24Name("活跃用户数");
				index.setIndex24Flag("Y");
			}
			// 如果有活跃率权限
			if ("25".equals(stringList.get(i))) {
				index.setIndex25Name("活跃率");
				index.setIndex25Flag("Y");
			}
			// 如果有沉默用户数权限
			if ("26".equals(stringList.get(i))) {
				index.setIndex26Name("沉默用户数");
				index.setIndex26Flag("Y");
			}
			// 如果有沉默率权限
			if ("27".equals(stringList.get(i))) {
				index.setIndex27Name("沉默率");
				index.setIndex27Flag("Y");
			}
			// 如果有存留量权限
			if ("28".equals(stringList.get(i))) {
				index.setIndex28Name("存留量");
				index.setIndex28Flag("Y");
			}
			// 如果有存留率权限
			if ("29".equals(stringList.get(i))) {
				index.setIndex29Name("存留率");
				index.setIndex29Flag("Y");
			}
			// 如果有上网人数权限
			if ("30".equals(stringList.get(i))) {
				index.setIndex30Name("上网人数");
				index.setIndex30Flag("Y");
			}
			// 如果有新上网人数权限
			if ("31".equals(stringList.get(i))) {
				index.setIndex31Name("新上网人数");
				index.setIndex31Flag("Y");
			}
			// 如果有上网平均时长权限
			if ("32".equals(stringList.get(i))) {
				index.setIndex32Name("上网平均时长");
				index.setIndex32Flag("Y");
			}
			// 如果有上网平均次数权限
			if ("33".equals(stringList.get(i))) {
				index.setIndex33Name("上网平均次数");
				index.setIndex33Flag("Y");
			}
			// 如果有上网人数占比权限
			if ("34".equals(stringList.get(i))) {
				index.setIndex34Name("上网人数占比");
				index.setIndex34Flag("Y");
			}
			// 如果有连接用户数权限
			if ("35".equals(stringList.get(i))) {
				index.setIndex35Name("连接用户数");
				index.setIndex35Flag("Y");
			}
			// 如果有新增连接用户数权限
			if ("36".equals(stringList.get(i))) {
				index.setIndex36Name("新增连接用户数");
				index.setIndex36Flag("Y");
			}
			// 如果有首次到店人数权限
			if ("37".equals(stringList.get(i))) {
				index.setIndex37Name("首次到店人数");
				index.setIndex37Flag("Y");
			}
			// 如果有首次到店率权限
			if ("38".equals(stringList.get(i))) {
				index.setIndex38Name("首次到店率");
				index.setIndex38Flag("Y");
			}
			// 如果有wifi开启率权限
			if ("39".equals(stringList.get(i))) {
				index.setIndex39Name("wifi开启率");
				index.setIndex39Flag("Y");
			}
			// 如果有历史用户数权限
			if ("40".equals(stringList.get(i))) {
				index.setIndex40Name("历史用户数");
				index.setIndex40Flag("Y");
			}
			// 如果有客流人次权限
			if ("41".equals(stringList.get(i))) {
				index.setIndex41Name("客流人次");
				index.setIndex41Flag("Y");
			}
			// 如果有游逛深度权限
			if ("42".equals(stringList.get(i))) {
				index.setIndex42Name("游逛深度");
				index.setIndex42Flag("Y");
			}
			// 如果有会员总数权限
			if ("43".equals(stringList.get(i))) {
				index.setIndex43Name("会员总数");
				index.setIndex43Flag("Y");
			}
			// 如果有服务能力权限
			if ("44".equals(stringList.get(i))) {
				index.setIndex44Name("服务能力");
				index.setIndex44Flag("Y");
			}
			// 如果有交易笔数权限
			if ("45".equals(stringList.get(i))) {
				index.setIndex45Name("交易笔数");
				index.setIndex45Flag("Y");
			}
			// 如果有平均活跃天数权限
			if ("46".equals(stringList.get(i))) {
				index.setIndex46Name("平均活跃天数");
				index.setIndex46Flag("Y");
			}
		}
		return index;
	}

	/**
	 * 计算指标
	 * 
	 * @param dataList
	 * @param siteIndex
	 * @return
	 */
	public static SiteIndex mathIndex(List<ReportGraphData> dataList,
			SiteIndex siteIndex) {
		double long0 = 0;
		double long1 = 0;
		double long2 = 0;
		double long3 = 0;
		double long4 = 0;
		double long5 = 0;
		double long6 = 0;
		double long7 = 0;
		double long8 = 0;
		double long9 = 0;
		double long10 = 0;
		double long11 = 0;
		double long12 = 0;
		double long13 = 0;
		double long14 = 0;
		double long15 = 0;
		double long16 = 0;
		double long17 = 0;
		double long18 = 0;
		double long19 = 0;
		double long20 = 0;
		double long21 = 0;
		double long22 = 0;
		double long23 = 0;
		double long24 = 0;
		double long25 = 0;
		double long26 = 0;
		double long27 = 0;
		double long28 = 0;
		double long29 = 0;
		double long30 = 0;
		double long31 = 0;
		double long32 = 0;
		double long33 = 0;
		double long34 = 0;
		double long35 = 0;
		double long36 = 0;
		double long37 = 0;
		double long38 = 0;
		double long39 = 0;
		double long40 = 0;
		double long41 = 0;
		double long42 = 0;
		double long43 = 0;
		double long44 = 0;
		double long45 = 0;
		double long46 = 0;
		int i = 0;
		for (i = 0; i < dataList.size(); i++) {
			long0 += dataList.get(i).getInnum();
			long1 += Double.parseDouble(dataList.get(i).getJkl());
			long2 += Double.parseDouble(dataList.get(i).getPosStaff());
			long3 += Double.parseDouble(dataList.get(i).getPos());
			long4 += Double.parseDouble(dataList.get(i).getRateBags());
			long5 += Double.parseDouble(dataList.get(i).getPosInnum());
			long6 += Double.parseDouble(dataList.get(i).getPosArea());
			long7 += Double.parseDouble(dataList.get(i).getCustomerNum());
			long8 += Double.parseDouble(dataList.get(i).getRetentionavgStaff());
			long9 += Double.parseDouble(dataList.get(i).getNewCustomerNum());
			long10 += Double.parseDouble(dataList.get(i).getOldCustomer());
			long11 += Double.parseDouble(dataList.get(i).getNewCustomerPro());
			long12 += Double.parseDouble(dataList.get(i).getOldCustomerPro());
			long13 += Double.parseDouble(dataList.get(i).getInStoreRates());// 进店率
			long14 += Double.parseDouble(dataList.get(i).getReturnStoreRates());
			long15 += Double.parseDouble(dataList.get(i).getLossUserNum());
			long16 += Double.parseDouble(dataList.get(i).getLossRates());
			long17 += Double.parseDouble(dataList.get(i).getBackUserNum());
			long18 += Double.parseDouble(dataList.get(i).getBackRates());
			long19 += Double.parseDouble(dataList.get(i).getMemNum());
			long20 += Double.parseDouble(dataList.get(i).getMemNumPro());
			long21 += Double.parseDouble(dataList.get(i).getRetentionavg());
			// 2015年12月11日13:59:59新增滞留量、出客流量
			long22 += Double.parseDouble(dataList.get(i).getRetentionNum());
			long23 += Double.parseDouble(dataList.get(i).getOutnum());
			// 2015年12月28日09:52:52新增9、10、11、12、14、19、24、26、30、31、32
			long24 += Double.parseDouble(dataList.get(i).getActiveUserNum());
			long25 += Double.parseDouble(dataList.get(i).getActiveRates());
			long26 += Double.parseDouble(dataList.get(i).getSilenceUserNum());
			long27 += Double.parseDouble(dataList.get(i).getSilenceRates());
			long28 += Double.parseDouble(dataList.get(i).getStayUserNum());
			long29 += Double.parseDouble(dataList.get(i).getStayRates());
			long30 += Double.parseDouble(dataList.get(i).getInterNum());
			long31 += Double.parseDouble(dataList.get(i).getNewInterNum());
			long32 += Double.parseDouble(dataList.get(i).getInterAvgDuration());
			long33 += Double.parseDouble(dataList.get(i).getInterAvgCount());
			long34 += Double.parseDouble(dataList.get(i).getConnectRates());
			long35 += Double.parseDouble(dataList.get(i).getConUserNum());
			long36 += Double.parseDouble(dataList.get(i).getNewAddUserNum());
			long37 += Double.parseDouble(dataList.get(i).getFirstArrNum());
			long38 += Double.parseDouble(dataList.get(i).getFirstArrRates());
			long39 += Double.parseDouble(dataList.get(i).getWifiOpenRate());
			long40 += Double.parseDouble(dataList.get(i).getHisUserNum());
			long41 += Double.parseDouble(dataList.get(i).getInCount());
			long42 += Double.parseDouble(dataList.get(i).getWanderDepth());
			long43 = Double.parseDouble(dataList.get(i).getMemTotalNum());// 获取最后一天的数据（会员总数）
			long44 += Double.parseDouble(dataList.get(i).getServiceAbility());
			long45 += Double.parseDouble(dataList.get(i).getPosnum());
			long46 += Double.parseDouble(dataList.get(i).getAvgActDayNum());
		}
		// 计算指标0-客流量(求和)
		siteIndex.setIndex0(revert0((Double.toString(long0))));
		// 计算指标1-集客力(平均)
		siteIndex.setIndex1(revert0(Double.toString(long1 / i)));
		// 计算指标2-人效(平均)
		siteIndex.setIndex2(revert(Double.toString(long2 / i)));
		// 计算指标3-销售额(求和)
		siteIndex.setIndex3(revert((Double.toString(long3))));
		// 计算指标4-提袋率(平均)
		siteIndex.setIndex4(revert(Double.toString(long4 / i)));
		// 计算指标5-客单价(平均)
		siteIndex.setIndex5(revert(Double.toString(long5 / i)));
		// 计算指标6-坪效(平均)
		siteIndex.setIndex6(revert(Double.toString(long6 / i)));
		// 计算指标7-顾客数(平和)
		siteIndex.setIndex7(revert0(Double.toString(long7)));
		// 计算指标8-平均游逛时间(平均)
		siteIndex.setIndex8(revert0(Double.toString(long8 / i)));
		siteIndex.setIndex9(revert0(Double.toString(long9)));// 求和
		siteIndex.setIndex10(revert0(Double.toString(long10)));// 求和
		siteIndex.setIndex11(revert(Double.toString(long11 / i)));
		siteIndex.setIndex12(revert(Double.toString(long12 / i)));
		siteIndex.setIndex13(revert(Double.toString(long13 / i)));
		siteIndex.setIndex14(revert(Double.toString(long14 / i)));
		siteIndex.setIndex15(revert0(Double.toString(long15)));
		siteIndex.setIndex16(revert(Double.toString(long16 / i)));
		siteIndex.setIndex17(revert0(Double.toString(long17)));
		siteIndex.setIndex18(revert(Double.toString(long18 / i)));
		siteIndex.setIndex19(revert0(Double.toString(long19)));// 求和
		siteIndex.setIndex20(revert(Double.toString(long20 / i)));// 求和
		// 计算指标21-平均滞留时间(平均)
		siteIndex.setIndex21(revert0(Double.toString(long21 / i)));
		// 2015年12月11日14:01:41新增滞留量、出客流量
		// 计算指标22-滞留量(平均)
		siteIndex.setIndex22(revert0(Double.toString(long22 / i)));
		// 计算指标23-出客流量(求和)
		siteIndex.setIndex23(revert0(Double.toString(long23)));
		// 2015年12月28日09:59:33新增9、10、11、12、14、19、24、26、30、31、32
		siteIndex.setIndex24(revert0(Double.toString(long24)));// 求和
		siteIndex.setIndex25(revert(Double.toString(long25 / i)));// 求和
		siteIndex.setIndex26(revert0(Double.toString(long26)));// 求和
		siteIndex.setIndex27(revert(Double.toString(long27 / i)));// 求和
		siteIndex.setIndex28(revert0(Double.toString(long28)));// 求和
		siteIndex.setIndex29(revert(Double.toString(long29 / i)));// 求和
		siteIndex.setIndex30(revert0(Double.toString(long30)));// 求和
		siteIndex.setIndex31(revert0(Double.toString(long31)));// 求和
		siteIndex.setIndex32(revert0(Double.toString(long32 / i)));
		siteIndex.setIndex33(revert0(Double.toString(long33 / i)));
		siteIndex.setIndex34(revert(Double.toString(long34 / i)));
		siteIndex.setIndex35(revert0(Double.toString(long35)));
		siteIndex.setIndex36(revert0(Double.toString(long36)));
		siteIndex.setIndex37(revert0(Double.toString(long37)));
		siteIndex.setIndex38(revert(Double.toString(long38 / i)));
		siteIndex.setIndex39(revert(Double.toString(long39 / i)));
		siteIndex.setIndex40(revert0(Double.toString(long40)));
		siteIndex.setIndex41(revert0(Double.toString(long41)));
		siteIndex.setIndex42(revert0(Double.toString(long42 / i)));
		siteIndex.setIndex43(revert0(Double.toString(long43)));
		siteIndex.setIndex44(revert0(Double.toString(long44 / i)));
		siteIndex.setIndex45(revert0(Double.toString(long45)));
		siteIndex.setIndex46(revert0(Double.toString(long46 / i)));
		return siteIndex;
	}

	// 保留两位小数
	private static String revert(String string) {
		BigDecimal b = new BigDecimal(string);
		DecimalFormat d1 = new DecimalFormat("#,##0.00");
		return d1.format(b);
	}

	// 取整数
	private static String revert0(String string) {
		BigDecimal b = new BigDecimal(string);
		DecimalFormat d1 = new DecimalFormat("#,##0");
		return d1.format(b);
	}

	/**
	 * 判断当前选中的广场是否属于真正有权限的广场，否则是根据店铺反查的广场，返回false
	 * 
	 * @param gcList
	 * @param gc
	 * @return
	 */
	public static boolean isHaveGcPer(List<SiteAllInfo> gcList, String gc) {
		boolean flag = false;
		for (int i = 0; i < gcList.size(); i++) {
			if (gc.equals(gcList.get(i).getSiteid())) {
				flag = true;
				break;
			}
		}
		return flag;
	}

}
